package com.xiaotu.service.sync;

import cn.hutool.core.collection.CollUtil;
import com.xiaotu.dao.mysql.MysqlStarWorksMapper;
import com.xiaotu.dao.pgsql.PgsqlStarWorksMapper;
import com.xiaotu.entity.DirectorWorksModel;
import com.xiaotu.entity.ScreenwriterWorksModel;
import com.xiaotu.entity.TvActorsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangxiao
 * @create 2021-09-23 11:18
 */
@Service
public class StarWorksService {

    @Autowired
    private PgsqlStarWorksMapper pgsqlStarWorksMapper;

    @Autowired
    private MysqlStarWorksMapper mysqlStarWorksMapper;

    private static Integer pageCount = 5000;

    @Transactional
    public void syncTvActors() {
        mysqlStarWorksMapper.clearTvActors();
        AtomicInteger pageNum = new AtomicInteger();
        while (true) {
            List<TvActorsModel> tvActorsList = pgsqlStarWorksMapper.getTvActors(pageNum.getAndIncrement(), pageCount);
            if (CollUtil.isNotEmpty(tvActorsList)) {
                mysqlStarWorksMapper.batchInsertTvActors(tvActorsList);
            } else {
                break;
            }
        }
    }

    @Transactional
    public void syncDirectorWorks() {
        mysqlStarWorksMapper.clearDirectorWorks();
        AtomicInteger pageNum = new AtomicInteger();
        while (true) {
            List<DirectorWorksModel> directorWorksList = pgsqlStarWorksMapper.getDirectorWorks(pageNum.getAndIncrement(), pageCount);
            if (CollUtil.isNotEmpty(directorWorksList)) {
                mysqlStarWorksMapper.batchInsertDirectorWorks(directorWorksList);
            } else {
                break;
            }
        }

    }

    @Transactional
    public void syncScreenwriterWorks() {
        mysqlStarWorksMapper.clearScreenwriterWorks();
        AtomicInteger pageNum = new AtomicInteger();
        while (true) {
            List<ScreenwriterWorksModel> screenwriterWorksList = pgsqlStarWorksMapper.getScreenwriterWorks(pageNum.getAndIncrement(), pageCount);
            if (CollUtil.isNotEmpty(screenwriterWorksList)) {
                mysqlStarWorksMapper.batchInsertScreenwriterWorks(screenwriterWorksList);
            } else {
                break;
            }
        }
    }

}
